/* Third-party library include */
#include <stdio.h>
#include <errno.h>
#include <unistd.h> 
/* project include             */
#include "config.h"
#include "link_socket.h"
#include "host_def.h"
#include "device_def.h"
#include "device_demo.h"
zlog_category_t     *gZlog;
INI_NODE            *iniConfigNode;

int initAppLog(const char* zlogConFile, char* cName)
{
    int ret = -1;
    ret = zlog_init(zlogConFile);
    if (ret){
        printf("FINE:[%s], FUNC:[%s], LINE:[%d] error:zlog init failed, zlogConFile:[%s], ret:[%d]\n", \
        __FILE__, __func__, __LINE__, zlogConFile, ret);
        return -1;
    }
	gZlog = zlog_get_category(cName);
	if (!gZlog) {
		printf("FINE:[%s], FUNC:[%s], LINE:[%d] error:get [%s] fail\n", \
        __FILE__, __func__, __LINE__, cName);
		zlog_fini();
		return -EFAULT;
	}
	zlog_debug(gZlog, "zlog init success");
    return 0;
}

int initAppIniConf(const char* iniConFile)
{
    int ret= -1;
    ret = ini_new(&iniConfigNode);
    if(ret){
        zlog_error(gZlog, "failed, return[%d]", ret);
        return -ret;
    }
    ret = ini_load(&iniConfigNode, iniConFile);
    if(ret){
        zlog_error(gZlog, "failed to load [%s]", iniConFile);
        return -ret;
    }
    return 0;
}

int loadDevConfig(void)
{
  int       ret = -1;
  int       devCnt = 0;
  char      configNodeSection[16] = {0};
  devInfo_t devInfo;

  ret = ini_get_int(iniConfigNode, "acqterinfo", "acqNodeNum", &devCnt);
  zlog_info(gZlog, "[%d] device in system", devCnt);
  for(int i = 1; i <= devCnt; i++){
    sprintf(configNodeSection, "acqNode%02d", i);
    ret |= ini_get_string(iniConfigNode, configNodeSection, "boardName", devInfo.devHwInfo.devBoardName);
    ret |= ini_get_string(iniConfigNode, configNodeSection, "fwVersion", devInfo.devHwInfo.devFwInfo);
    ret |= ini_get_string(iniConfigNode, configNodeSection, "ipAddr", devInfo.devNetInfo.devEthLinkInfo.ipAddr);
    ret |= ini_get_string(iniConfigNode, configNodeSection, "mask", devInfo.devNetInfo.devEthLinkInfo.mask);
    ret |= ini_get_string(iniConfigNode, configNodeSection, "gateway", devInfo.devNetInfo.devEthLinkInfo.gateway);
    ret |= ini_get_string(iniConfigNode, configNodeSection, "mac", devInfo.devNetInfo.devEthLinkInfo.mac);
    ret |= ini_get_int(iniConfigNode, configNodeSection, "port", (int*)&devInfo.devNetInfo.devEthLinkInfo.port);
    if(ret == 0){
      zlog_info(gZlog, "get acqNode[%d] Info:       \n\
          boardName[%s]                             \n\
          fwVersion[%s]                             \n\
          ipAddr[%s]                                \n\
          mask[%s]                                  \n\
          gateway[%s]                               \n\
          mac[%s]                                   \n\
          port[%d]                                  \n\
          ret[%d]",                                   \
          i,                                          \
          devInfo.devHwInfo.devBoardName,             \
          devInfo.devHwInfo.devFwInfo,                \
          devInfo.devNetInfo.devEthLinkInfo.ipAddr,   \
          devInfo.devNetInfo.devEthLinkInfo.mask,     \
          devInfo.devNetInfo.devEthLinkInfo.gateway,  \
          devInfo.devNetInfo.devEthLinkInfo.mac,      \
          devInfo.devNetInfo.devEthLinkInfo.port,     \
          ret);
      devInfo.devId = i;
      ret = addDev(i, devInfo);
    }else{
      zlog_error(gZlog, "get acqNode[%d] failed", i);
    }
  }
  return ret;
}

int commonInit(void)
{
  int ret = -1;
  int devCnt = 0;
  char acqInfo[3][32];

  ret = initAppLog(LOG_CONFIG_PATH, LOG_APP_CNAME);
  zlog_debug(gZlog, "starting zloging for acquisition terminal...");
  ret = initAppIniConf(INI_CONFIG_PATH);  
  ret = ini_get_string(iniConfigNode, "acqterinfo", "boardName", acqInfo[0]);
  ret = ini_get_string(iniConfigNode, "acqterinfo", "swVersion", acqInfo[1]);
  ret = ini_get_int(iniConfigNode, "acqterinfo", "acqNodeNum", &devCnt);
  zlog_info(gZlog, "get acqsition terminal info:\n\
  board name:[%s]\n\
  sw version:[%s]\n\
  ret       :[%d]", acqInfo[0], acqInfo[1], ret);
  /* device manage */
  devProtocolInit();
  ret |= loadDevConfig();
  /* print device info */
  ret |= printDev();
  /* start up business */
  ret |=devCommPthInit(devCnt);
  /* host server */ 
  ret = tcpServerInit(hostCmdExe);
  return 0;
}

